package service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.Admin_dao;
import dao.Menu_dao;
import dao.RoleGroup_dao;
import dto.AdminDto;
import dto.Admin_MenuDto;
import entity.Admin;
import entity.Group_Menu;
import entity.Menu;
import service.Admin_service;

@Service
public class Admin_service_impl implements Admin_service {

	@Autowired
	private Admin_dao admin_dao;
	@Autowired
	private RoleGroup_dao rolegroup_dao;
	@Autowired
	private Menu_dao menu_dao;
	
	@Override
	public boolean add_admin(Admin admin) {
		// TODO Auto-generated method stub
		return admin_dao.add_admin(admin);
	}

	@Override
	public boolean delete_admin(Integer id) {
		// TODO Auto-generated method stub
		return admin_dao.delete_admin(id);
	}

	@Override
	public boolean update_admin(Admin admin) {
		// TODO Auto-generated method stub
		return admin_dao.update_admin(admin);
	}

	@Override
	public Admin get_admin(Admin admin) {
		// TODO Auto-generated method stub
		return admin_dao.get_admin(admin);
	}

	@Override
	public List<AdminDto> adminList() {
		List<AdminDto> result=new ArrayList<>();
		List<Admin> list=admin_dao.adminList();
		for (Admin admin : list) {
			AdminDto admindto=new AdminDto();
			BeanUtils.copyProperties(admin, admindto);
			admindto.setpId(0);
			result.add(admindto);
		}
		return result;
	}

	@Override
	public List<Admin_MenuDto> get_AdminMenu(Admin admin) {
		if(admin!=null){
			Integer groupId=admin.getGroupId();
			List<Group_Menu> groupmenuList=rolegroup_dao.Group_MenuList(groupId);
			List<Admin_MenuDto> AdminMenuDtoList=new ArrayList<>();
			for (Group_Menu group_Menu : groupmenuList) {
				Menu menu=menu_dao.get_MenuById(group_Menu.getMenuId());
				if(menu.getParentId()==0){
					Admin_MenuDto admin_MenuDto=new Admin_MenuDto();
					admin_MenuDto.setParentMenu(menu);
					List<Menu> childMenuList=menu_dao.get_childMenuByParentId(menu.getId()); //查找子菜单
					admin_MenuDto.setChildMenuList(childMenuList);
					AdminMenuDtoList.add(admin_MenuDto);
				}
			}
			return AdminMenuDtoList;
		}
		return null;
	}

	@Override
	public Admin getAdminByName(String name) {
		return admin_dao.getAdminByName(name);
	}

	@Override
	public Admin getAdminById(Integer id) {
		return admin_dao.getAdminById(id);
	}

}
